<script setup lang="ts">
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showToast } from 'vant'
import { ref } from 'vue'
import { loginByMobile, loginByPassword } from '@/services/user'
import { useUserStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'
import { useSendMobileCode } from '@/composable'
import axios from 'axios'

const mobile = ref('')
const password = ref('')
const agree = ref(false)

const store = useUserStore()
const router = useRouter() // 整个项目中唯一的路由实例对象(跳转)
const route = useRoute() // 获取当前页面对应的路由对象(获取传递的参数)

// 1. 登录
const login = async () => {
  // 1.1 判断用户是否勾选同意协议
  if (!agree.value) return showToast('请勾选同意用户协议')

  // 1.2 调用接口函数进行登录操作
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByMobile(mobile.value, code.value)

  store.setUser(res.data)
  // 1.3 判断: 如果有回跳地址则按回跳地址跳转, 如果没有则去个人中心 /user
  router.replace((route.query.returnUrl as string) || '/user')
  showToast({ message: '登录成功' })
}

// 2. 短信登录-切换效果
const isPass = ref(true)
const code = ref('')

// 3. 发送短信验证码
const { time, send, formRef } = useSendMobileCode(mobile, 'login')

// 4. 使用svg图标
const isShow = ref(false)

// 5. qq登录
const qqUrl = `https://graph.qq.com/oauth2.0/authorize?client_id=102015968&response_type=token&scope=all&redirect_uri=${encodeURIComponent(
  import.meta.env.VITE_APP_CALLBACK + '/login/callback'
)}`

axios.get('/patient/message/sys/list').then((res) => {
  console.log(res.data)
})
</script>

<template>
  <div class="login-page">
    <cp-nav-bar
      right-text="注册"
      @click-right="$router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ isPass ? '短信验证码登录' : '密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form ref="formRef" autocomplete="off" @submit="login">
      <van-field
        name="mobile"
        v-model="mobile"
        placeholder="请输入手机号"
        type="tel"
        :rules="mobileRules"
      ></van-field>
      <!-- 密码登录 -->
      <van-field
        v-if="isPass"
        v-model="password"
        placeholder="请输入密码"
        :type="`${isShow ? 'text' : 'password'}`"
        :rules="passwordRules"
      >
        <template #button>
          <cp-icon
            :name="`login-eye-${isShow ? 'on' : 'off'}`"
            @click="isShow = !isShow"
          />
        </template>
      </van-field>
      <!-- 验证码登录 -->
      <van-field
        v-else
        v-model="code"
        placeholder="请输入短信验证码"
        :rules="codeRules"
      >
        <!-- 尾部按钮 -->
        <template #button>
          <span class="btn-send" :class="{ active: time > 0 }" @click="send">
            {{ time > 0 ? `${time}s后再次发送` : '发送验证码' }}
          </span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button native-type="submit" block round type="primary"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>

      <a
        class="icon"
        :href="qqUrl"
        @click="store.setReturnUrl(route.query.returnUrl as string)"
      >
        <img src="@/assets/qq.svg" alt="" />
      </a>

      <!-- <div class="icon" @click="qqLogin">
        <img src="@/assets/qq.svg" alt="" />
      </div> -->
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/login.scss';
</style>
